package com.osfw.dbadminer.service.connect.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.dbadminer.entity.config.DbaConfigScope;
import com.osfw.dbadminer.entity.connect.DbaConnect;
import com.osfw.dbadminer.mapper.connect.DbaConnectMapper;
import com.osfw.dbadminer.service.config.IDbaConfigScopeService;
import com.osfw.dbadminer.service.connect.IDbaConnectService;
import com.osfw.dbadminer.service.jdbc.JdbcBaseFactory;
import com.osfw.dbadminer.service.jdbc.JdbcBaseService;
import com.osfw.framework.core.domain.Ztree;
import com.osfw.system.entity.manage.SysDept;
import com.osfw.system.entity.manage.SysMenu;
import com.osfw.system.entity.manage.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 连接配置管理 服务层处理
 */
@Service
public class DbaConnectServiceImpl extends ServiceImpl<DbaConnectMapper, DbaConnect> implements IDbaConnectService {

    @Autowired
    private IDbaConfigScopeService configScopeService;

    @Override
    public List<DbaConnect> getConnects() {
        LambdaQueryWrapper<DbaConnect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DbaConnect::getId, DbaConnect::getGroupFlag, DbaConnect::getType, DbaConnect::getName);
        queryWrapper.orderByAsc(DbaConnect::getGroupFlag, DbaConnect::getType, DbaConnect::getName);
        return list(queryWrapper);
    }

    @Override
    public List<DbaConnect> getConnectsByIds(List<Long> connectIds, SysUser sysUser) {
        if(sysUser.isAdmin()) {
            LambdaQueryWrapper<DbaConnect> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(DbaConnect::getGroupFlag, DbaConnect::getType, DbaConnect::getName);
            return list(queryWrapper);
        }else {
            if(connectIds.isEmpty()) {
                return new ArrayList<>();
            }else {
                LambdaQueryWrapper<DbaConnect> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(DbaConnect::getId, connectIds);
                queryWrapper.orderByAsc(DbaConnect::getGroupFlag, DbaConnect::getType, DbaConnect::getName);
                return list(queryWrapper);
            }
        }

    }

    @Override
    public List<Ztree> databaseTreeImmediateChild(Long parentId, SysUser sysUser) {
        if (parentId == null) {
            parentId = 0l;
        }
        List<Ztree> ztrees = new ArrayList<Ztree>();
        if(parentId == 0l) {
            List<Long> connectIds = configScopeService.selectConnectIdsByUser(sysUser);
            List<DbaConnect> connects = getConnectsByIds(connectIds, sysUser);
            connects.stream().forEach(connect -> {
                Ztree ztree = new Ztree();
                ztree.setId(connect.getId());
                ztree.setpId(0l);
                ztree.setName(connect.getName());
                ztree.setTitle(connect.getGroupName());
                // 是否父节点
                ztree.setIsParent("true");
                ztrees.add(ztree);
            });
        }else {
            DbaConnect connect = getById(parentId);
            JdbcBaseService jdbcService = JdbcBaseFactory.createAndInit(connect);
            List<String> databases = new ArrayList<>();
            try {
                databases = jdbcService.getDatabaseList().stream().sorted().collect(Collectors.toList());
            } catch (Exception e) {
                log.error("数据库连接异常", e);
                databases.add("数据库连接异常!");
            }
            List<String> databaseNames = configScopeService.selectDatabaseNamesByConnectAndUser(parentId, sysUser);
            databases = databases.stream().filter(x -> sysUser.isAdmin() || databaseNames.contains(x)).collect(Collectors.toList());
            for (int i = 0; i <= databases.size() - 1; i++) {
                Ztree ztree2 = new Ztree();
                ztree2.setId(connect.getId() + i);
                ztree2.setpId(connect.getId());
                if(databases.get(i).equals("数据库连接异常!")){
                    ztree2.setName("<i class=\"am-text-danger\" >"+databases.get(i)+"</i>");
                }else {
                    ztree2.setName(databases.get(i));
                }
                ztree2.setTitle(databases.get(i));
                // 是否父节点
                ztree2.setIsParent("false");
                ztrees.add(ztree2);
            }
        }
        return ztrees;
    }

}
